package com.ossean.match.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ossean.match.model.Project;

public interface ProjectDao {

	/*
	 * 获得项目数据库中最新数据中的最大的id返回Integer而不是int 是为了防止在项目数为0时，sql返回null 与int类型不匹配
	 */
	/*
	 * @Select("select MAX(id) from projects") public Integer getNewLast();
	 * 
	 * @Select(
	 * "select id,name from projects where id>#{prjHistory} AND id<=#{prjNewLast}"
	 * ) public List<Project> getBatch(@Param("prjHistory")int
	 * prjHistory,@Param("prjNewLast") int prjNewLast);
	 */

	@Select("select count(id) from open_source_projects where (filtration = 1) or (update_mark is not null and filtration = 2)")
	public Integer getNewPrjCount();

	@Select("select MAX(id) from open_source_projects")
	public Integer getNewLast();

	// @Select("select id,name,url from open_source_projects where id>#{prjHistory} AND id<=#{prjNewLast}")
	// public List<Project> getBatch(@Param("prjHistory")int
	// prjHistory,@Param("prjNewLast") int prjNewLast);

	// @Select("select * from ${table} limit #{position}, 1")
	// public TestResult1 getByPosition(@Param("table") String table,
	// @Param("position") int position);

	// 根据id获得项目对象
	@Select("select id,filtration from open_source_projects where id = #{prjId}")
	public Project getPrjById(@Param("prjId") int prjId);

	@Select("select min(rank) as rank, min(rank_star) as rank_star from ${table} where Url = #{url}")
	public Project getProjByUrl(@Param("table") String table,
			@Param("url") String url);

	// 批量获取项目对象
	@Select("select id,name,url,tags,synonyms from open_source_projects where id>#{startId} AND id<=#{endId} AND ((filtration = 1) or (update_mark is not null and filtration = 2))")
	public List<Project> getBatchPrjs(@Param("startId") int startId,
			@Param("endId") int endId);

	// 批量获取项目，filtration为1表示保留，为2表示之前保留的且已处理，为0标识不保留；update_mark为null表示已处理或者无更新，不为null表示有更新
	@Select("select id,name,url,tags,synonyms from open_source_projects where id>#{startId} AND id<=#{endId} AND ((filtration = 1) or (update_mark is not null and filtration = 2)) limit #{batchSize}")
	public List<Project> getBatchPrjsIncre(@Param("startId") int startId,
			@Param("endId") int endId,@Param("batchSize") int batchSize);
	
	@Select("select id,name,url,tags,synonyms from open_source_projects where (filtration = 1) or (update_mark is not null and filtration = 2) limit #{batchSize}")
	public List<Project> getBatchPrjsIncreWithoutLimit(@Param("batchSize") int batchSize);

	// 将已经匹配过的项目的update_mark和filtration设为null和2，表示已匹配
	@Update("update open_source_projects set update_mark = null,filtration = 2 where id = #{prjId}")
	public void updateProcessedPrj(@Param("prjId") int prjId);
}
